<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 * 产品分类
 */
 
namespace osc\mobile\controller;
use think\Db;
class Category extends MobileBase
{
	function index(){
		$type=input('param.type');

		$top_title = '平台商城';

		$flag = 'plat';

		if($type == 2){
			$top_title = '报单商城';
			$flag = 'service';
		}

		if($type == 3){
			$top_title = '特色商城';
			$flag = 'form';
		}

		$tree= new \oscshop\Tree();

		$list = Db::name('category')->where('pid',0)->field('id,pid,name')->order('sort_order asc')->select();

		$category = $tree->toOdArray($list);

		//获取公告
		$art1 = Db::name('article')->order('id desc')->where('status',1)->find();

		$this->assign('art1', $art1);

		$this->assign('slide', osc_system()->get_plugin_slide());
		$this->assign('category',$category);
		$this->assign('top_title',$top_title);
		$this->assign('type',$type?$type:1);

		$this->assign('flag',$flag);
		$this->assign('SEO',['title'=>'商城-'.config('SITE_TITLE')]);
		
		return $this->fetch();
	}
	
	//取出分类下所有数据，数据量大时候需要改造
	function get_goods(){
		
		$id=input('param.id');
		$type=input('param.type');

//		print_r($type);exit;

		$tree=new \oscshop\Tree();
		$child=$tree->getChildsId($this->get_category_tree(),$id);	
		//加入顶级分类	
		array_push($child,$id);	
		
		$map['GoodsToCategory.category_id']=array('in',$child);	
		
		$map['Goods.status']=array('eq',1);	
		$map['Goods.type']=array('eq',$type);
//		print_r($map);exit;
		$list= Db::view('Goods','goods_id,image,name,price')
		->view('GoodsToCategory','category_id','Goods.goods_id=GoodsToCategory.goods_id')					
		->where($map)->select();
		foreach($list as &$value){
			$img = '/'.$value['image'];
			$value['image'] = '/'.resize($img,250,250);
		}

		return $list;
	
	}
	public function get_category_tree(){	
		$tree=new \oscshop\Tree();	
		return $tree->toFormatTree(Db::name('category')->field('id,pid,name')->select(),'name');
	}
}
